<?php $this->_extends('_layouts/main_layout'); ?>
<?php $this->_block('title'); ?>
<?php echo isset($pub) ? "Publishers" : "Admins"; ?>
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>
<div id="content" class="box">
	<h1><?php echo "Settings"; ?></h1>
    <fieldset>
    	<legend>Offers Settings</legend>
        <form method="post" action="<?php echo url('admin/settings');?>">
            <table>
                <tbody>
                    <tr>
                    	<td>Cookie Window</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['cookie_window'] ?>" name="cookie_window"></td>
                    </tr>
                    <tr>
                    	<td>Ip Window</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['ip_window'] ?>" name="ip_window"></td>
                    </tr>
                    <tr>
                        <td class="t-right" colspan="2">
                        	<input class="input-submit" type="submit" value="Save">
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </fieldset>
    
    
    
    <fieldset>
    	<legend>Monetizations Settings</legend>
        <form method="post" action="<?php echo url('admin/settings');?>">
            <table>
                <tbody>
                    <tr>
                    	<td>Super Reward Token Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['super_reward_token_value'] ?>" name="super_reward_token_value"></td>
                    </tr>
                    <tr>
                    	<td>Peanut Labs Token Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['peanut_labs_token_value'] ?>" name="peanut_labs_token_value"></td>
                    </tr>
                    <tr>
                    	<td>Radium One Token Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['radium_one_token_value'] ?>" name="radium_one_token_value"></td>
                    </tr>
                    <tr>
                    	<td>Epic Social Token Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['epic_social_token_value'] ?>" name="epic_social_token_value"></td>
                    </tr>
                    <tr>
                    	<td>Fluent Coreg Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['fluent_coreg_value'] ?>" name="fluent_coreg_value"></td>
                    </tr>
                    <tr>
                    	<td>Prospectiv Coreg Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['prospectiv_coreg_value'] ?>" name="prospectiv_coreg_value"></td>
                    </tr>
                    <tr>
                    	<td>Ibid2save Value</td>
                        <td><input class="input-text" type="text" value="<?php echo $conf['ibid2save_value'] ?>" name="ibid2save_value"></td>
                    </tr>
                    <tr>
                        <td class="t-right" colspan="2">
                        	<input class="input-submit" type="submit" value="Save">
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </fieldset>
    
    
    
    
    
    <fieldset>
    	<legend>US Coreg Partners Settings</legend>
        <form method="post" action="<?php echo url('admin/settings');?>">
            <table>
                <tbody>
	                <tr>
	                	<th>Action</th>
	                	<th>Fluent</th>
	                	<th>Prospectiv</th>
	                	<th>Coolsavings</th>
	                	<th>Fluent Proportion</th>
	                	<th>Prospectiv Proportion</th>
	                	<th>Coolsavings Proportion</th>
	                </tr>
                    <tr>
                    	<td>Register</td>
                       	<td>
                    		<select class="input-text set_coreg_input" name="register_fluent_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>
                    	</td>
                    	<td>
                    		<select class="input-text set_coreg_input" name="register_prospectiv_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>	
                    	</td>
                    	<td>
                    		<select class="input-text set_coreg_input" name="register_coolsavings_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>	
                    	</td>
                    	<td><span id="register_fluent_rate"><?php echo Util::getProportionAB(2, $conf['register_fluent_value'], $conf['register_prospectiv_value'], $conf['register_coolsavings_value']); ?></span></td>
                    	<td><span id="register_prospectiv_rate"><?php echo Util::getProportionAB(2, $conf['register_prospectiv_value'], $conf['register_fluent_value'], $conf['register_coolsavings_value']); ?></span></td>
                    	<td><span id="register_coolsavings_rate"><?php echo Util::getProportionAB(2, $conf['register_coolsavings_value'], $conf['register_fluent_value'], $conf['register_prospectiv_value']); ?></span></td>
                    </tr>
                    <tr>
                    	<td>Profile</td>
                       	<td>
                    		<select class="input-text set_coreg_input" name="profile_fluent_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>
                    	</td>
                    	<td>
                    		<select class="input-text set_coreg_input" name="profile_prospectiv_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>	
                    	</td>
                    	<td>
                    		<select class="input-text set_coreg_input" name="profile_coolsavings_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>	
                    	</td>
                    	<td><span id="profile_fluent_rate"><?php echo Util::getProportionAB(2, $conf['profile_fluent_value'], $conf['profile_prospectiv_value'], $conf['profile_coolsavings_value']); ?></span></td>
                    	<td><span id="profile_prospectiv_rate"><?php echo Util::getProportionAB(2, $conf['profile_prospectiv_value'], $conf['profile_fluent_value'], $conf['profile_coolsavings_value']); ?></span></td>
                    	<td><span id="profile_coolsavings_rate"><?php echo Util::getProportionAB(2, $conf['profile_coolsavings_value'], $conf['profile_fluent_value'], $conf['profile_prospectiv_value']); ?></span></td>
                    </tr>
                    <tr>
                    	<td>Email Active</td>
                        <td>
                    		<select class="input-text set_coreg_input" name="emactive_fluent_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>
                    	</td>
                    	<td>
                    		<select class="input-text set_coreg_input" name="emactive_prospectiv_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>	
                    	</td>
                    	<td>
                    		<select class="input-text set_coreg_input" name="emactive_coolsavings_value">
                    			<?php for($i = 0; $i <= 10; $i++): ?>
                    				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    			<?php endfor; ?>
                    		</select>	
                    	</td>
                    	<td><span id="emactive_fluent_rate"><?php echo Util::getProportionAB(2, $conf['emactive_fluent_value'], $conf['emactive_prospectiv_value'], $conf['emactive_coolsavings_value']); ?></span></td>
                    	<td><span id="emactive_prospectiv_rate"><?php echo Util::getProportionAB(2, $conf['emactive_prospectiv_value'], $conf['emactive_fluent_value'], $conf['emactive_coolsavings_value']); ?></span></td>
                    	<td><span id="emactive_coolsavings_rate"><?php echo Util::getProportionAB(2, $conf['emactive_coolsavings_value'], $conf['emactive_fluent_value'], $conf['emactive_prospectiv_value']); ?></span></td>
                    </tr>
                    <tr>
                        <td class="t-right" colspan="7">
                        	<input class="input-submit" id="set_coreg_submit" type="submit" value="Save">
                        </td>
                    </tr>
                </tbody>
            </table>
			<script>
				Array.prototype.delByVal=function(val){
					var newArray=[];
					for (var i = 0, item; (item= this[i]) != null; i++) {
						if (item != val) {
					   		newArray.push(item);
					   	}
					}
					return newArray;
				}
			
				function getProportionAB()
				{
					var args = arguments;
					var a = args[0];
					var total = 0;

					for(var i=0; i<args.length; i++){
						total += args[i]*1;
					}

					total -= args[i-1];
					decimals = args[i-1];	
					
					if(total == 0){
						return 0 + '%';
					}else{
						return ((a*100/total).toFixed(decimals)) + '%';
					}
				}
			
				$("select[name='register_fluent_value']").val('<?php echo $conf['register_fluent_value']; ?>');
				$("select[name='register_prospectiv_value']").val('<?php echo $conf['register_prospectiv_value']; ?>');
				$("select[name='register_coolsavings_value']").val('<?php echo $conf['register_coolsavings_value']; ?>');
				
				$("select[name='profile_fluent_value']").val('<?php echo $conf['profile_fluent_value']; ?>');
				$("select[name='profile_prospectiv_value']").val('<?php echo $conf['profile_prospectiv_value']; ?>');
				$("select[name='profile_coolsavings_value']").val('<?php echo $conf['profile_coolsavings_value']; ?>');
				
				$("select[name='emactive_fluent_value']").val('<?php echo $conf['emactive_fluent_value']; ?>');
				$("select[name='emactive_prospectiv_value']").val('<?php echo $conf['emactive_prospectiv_value']; ?>');
				$("select[name='emactive_coolsavings_value']").val('<?php echo $conf['emactive_coolsavings_value']; ?>');

				$('.set_coreg_input').change(function(){
					var partnerArray = new Array('fluent', 'prospectiv', 'coolsavings');
					var action = $(this).attr('name').split('_')[0];
					var partner = $(this).attr('name').split('_')[1];
					
					var anotherPartners = partnerArray.delByVal(partner);
					
					var thisVal = $(this).val();
					var anotherVal_0 = $("select[name='"+action+"_"+anotherPartners[0]+"_value']").val();
					var anotherVal_1 = $("select[name='"+action+"_"+anotherPartners[1]+"_value']").val();

					$('#'+action+'_'+partner+'_rate').html(getProportionAB(thisVal, anotherVal_0, anotherVal_1, 2));
					$('#'+action+'_'+anotherPartners[0]+'_rate').html(getProportionAB(anotherVal_0, thisVal, anotherVal_1, 2));
					$('#'+action+'_'+anotherPartners[1]+'_rate').html(getProportionAB(anotherVal_1, thisVal, anotherVal_0, 2));
				});
			</script>
        </form>
    </fieldset>
<?php $this->_endblock(); ?>